package com.seveneight.baijiesteward.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.seveneight.baijiesteward.R;
import com.seveneight.baijiesteward.bean.BankCardListBean;
import com.seveneight.baijiesteward.bean.BankCardStyleListBean;
import com.seveneight.baijiesteward.bean.BankListBean;
import com.seveneight.baijiesteward.ui.main.activity.BankCardInfoActivity;
import com.seveneight.baijiesteward.ui.main.activity.SelectBankActivity;
import com.seveneight.baijiesteward.utils.StringUtils;
import com.seveneight.baijiesteward.views.cover.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 卡通道adapter
 * Created by user1 on 2018/5/31.
 */

public class SelectBankApplicationAdapter extends BaseAdapter {
    LayoutInflater mInflater;
    List<BankCardListBean> listData;
    Activity activity;
    public SelectBankApplicationAdapter(Activity activity, List<BankCardListBean> listData){
        mInflater = LayoutInflater.from(activity);
        this.activity=activity;
        this.listData=listData;
    }

    @Override
    public int getCount() {
        return null==listData?0:listData.size();
    }
    public void refresh(List<BankCardListBean> listData){
        this.listData=listData;
        this.notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return null==listData?0:listData.size();
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_select_bank_application_list_layout, null); //加载布局
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
            holder = (ViewHolder) convertView.getTag();
         }
        BankCardListBean item=listData.get(i);
        Glide.with(activity).load(item.getImgUrlPath()).into(holder.item_bank_photo);
        holder.itme_name.setText(item.getName());
        if (!StringUtils.isEmpty(item.getDescription())){
            String[] dataArray=item.getDescription().split("；");
            List<BankCardStyleListBean> data= new ArrayList<>();
            for (int n=0;n<dataArray.length;n++){
                if (n<3){
                    BankCardStyleListBean bean=new BankCardStyleListBean();
                    bean.setName(dataArray[n]);
                    data.add(bean);
                }
            }
            BankStyleAdapter mAdapter=new BankStyleAdapter();
            holder.bank_style_list.setAdapter(mAdapter);
            mAdapter.refresh(data);
            holder.bank_style_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int index, long l) {
                    Intent intent=new Intent(activity,BankCardInfoActivity.class);
                    intent.putExtra("StationChannelId",listData.get(i).getStationChannelId());
                    intent.putExtra("StationBankCardChannelId",listData.get(i).getStationBankCardChannelId());
                    activity.startActivityIfNeeded(intent,22);
                }
            });
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(activity,BankCardInfoActivity.class);
                intent.putExtra("StationChannelId",listData.get(i).getStationChannelId());
                intent.putExtra("StationBankCardChannelId",listData.get(i).getStationBankCardChannelId());
                activity.startActivityIfNeeded(intent,22);
            }
        });

        return convertView;
    }

    public class ViewHolder{
        ImageView item_bank_photo;
        TextView itme_name;
        MyListView bank_style_list;

        ViewHolder(View view){
            item_bank_photo=view.findViewById(R.id.item_bank_photo);
            itme_name=view.findViewById(R.id.itme_name);
            bank_style_list=view.findViewById(R.id.bank_style_list);
        }
    };

    class BankStyleAdapter extends BaseAdapter{
        List<BankCardStyleListBean> listBeans;

        public void refresh(List<BankCardStyleListBean> listData){
            this.listBeans=listData;
            this.notifyDataSetChanged();
        }
        @Override
        public int getCount() {
            return null==listBeans?0:listBeans.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {
            ViewHolder2 holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_bank_text_content_layout, null); //加载布局
                holder=new ViewHolder2(convertView);
                convertView.setTag(holder);
            } else {   //else里面说明，convertView已经被复用了，说明convertView中已经设置过tag了，即holder
                holder = (ViewHolder2) convertView.getTag();
            }

            holder.item_content.setText(listBeans.get(i).getName());
            return convertView;
        }
        public class ViewHolder2{
            TextView item_content;

            ViewHolder2(View view){
                item_content=view.findViewById(R.id.item_content);
            }
        };
    }
}

